<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>TitleAdd1</title>
</head>
<link href="./layui/css/layui.css" rel="stylesheet">
<body>
<div style="margin: 0 auto;width:70%;display: block">
    <div class="layui-form" action="" >
        <h1 id="show">新增二级标题</h1>
        <div class="layui-form-item">
            <label class="layui-form-label">二级标题章数</label>
            <div class="layui-input-block">
                <input type="text" name="d2"  lay-verify="required|number"  placeholder="请输入数字" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="zwbt" required  placeholder="请输入内容" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea name="zwnr" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo" style="display: none" id="submit">提交</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script src="./js/jquery-1.9.1.min.js"></script>
<script src="./layui/layui.js"></script>

<script>

    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for(var i = 0; i < strs.length; i ++) {
                theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

    var Request = new Object();
    Request = GetRequest();
    //$("#title").html(Request['id'])
    if (Request['d']){
        var loading = layer.load();
        var d=Request['d'];
        $.ajax({
            type: 'get',
            url: './titleList.php?titleType=d1&d='+d,
            success: function (data) {
                var jsonData = JSON.parse(data);
                console.log(jsonData)
                if (jsonData.status) {
                    $("#show").html(jsonData.msg["正文标题"])

                    layer.close(loading);
                } else {
                    layer.msg(jsonData.msg)
                    layer.close(loading);
                }
            },
            error: function () {
                layer.msg("请求异常")
                layer.close(loading);

            }
        });

    }else {
        layer.msg("异常！请重新进入！")
    }

    layui.use('form', function(){
        var form = layui.form;
        form.on('submit(formDemo)', function(data){
            if (data.field.zwnr){
                zwnr=data.field.zwnr.replace(/\n|\r\n/g,'\\r\\n');
            }
            zwbt=data.field.zwbt;
            d2=data.field.d2;
            localStorage.setItem(d+"-"+d2+"twoadd",zwnr);
            var params={
                d,zwbt,zwnr,d2,'type':"a2",
            }
            $.ajax({
                type:'post',
                url:'./zwToDB.php',
                data:params,
                success:function (data) {
                    var jsonData = JSON.parse(data);
                    if (jsonData.status){
                        alert(jsonData.msg)
                    }else {
                        alert(jsonData.msg)
                    }
                },
                error:function () {
                    layer.msg("请求异常")
                }
            });

        })
    })

    function submitting() {
        $("#submit").click()
    }



</script>